package com.hr.controller.admin;

import com.hr.domain.dto.AiHouseDTO;
import com.hr.domain.dto.HouseDTO;
import com.hr.domain.query.HousePageResult;
import com.hr.domain.query.HouseQueryParams;
import com.hr.domain.vo.AiHouseVO;
import com.hr.domain.vo.HouseVO;
import com.hr.service.HouseService;
import com.hr.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin")
@Tag(name = "后台房屋信息管理")
@AllArgsConstructor
public class HouseController {

    private final HouseService houseService;

    /**
     * 获取房屋列表
     * @return houseList
     */
    @GetMapping("/house/list")
    @Operation(summary = "获取房屋列表")
    public Result getHouseList() {
        log.info("获取房屋列表");
        List<HouseVO> houseList = houseService.getHouseList();
        return Result.success(houseList);
    }

    /**
     * 分页获取房屋列表
     * @param queryParams
     * @return housePageResult
     */
    @GetMapping("/house/page")
    @Operation(summary = "分页获取房屋列表")
    public Result getHousePageList(HouseQueryParams queryParams) {
        log.info("分页获取房屋列表");
        HousePageResult housePageResult = houseService.getHousePageList(queryParams);
        return Result.success(housePageResult);
    }

    /**
     * 上架、下架房屋
     * @param id
     * @param status
     * @return msg
     */

    @PutMapping("/house/status")
    @Operation(summary = "上架、下架房屋")
    public Result updateHouseStatus(@RequestParam("id") Long id,
                                    @RequestParam("status") Integer status) {
        if(status != 1 && status != 0){
            return Result.error("状态值错误,请重新输入");
        }
        log.info("上架、下架房屋");
        String msg = houseService.updateHouseStatus(id, status);
        return Result.success(msg);
    }

    /**
     * 发布房屋信息
     * @param houseDTO
     * @return msg
     */
    @PostMapping("/house/publish")
    @Operation(summary = "发布房屋信息")
    public Result publishHouse(@RequestBody HouseDTO houseDTO) {

        log.info("发布房屋信息");
        String msg = houseService.publishHouse(houseDTO);
        return Result.success(msg);
    }

    /**
     * 删除房屋信息
     * @param id
     * @return msg
     */
    @DeleteMapping("/house/delete/{id}")
    @Operation(summary = "删除房屋信息")
    public Result deleteHouse(@PathVariable("id") Integer id) {
        if (id == null){
            return Result.error("参数错误");
        }
        log.info("删除房屋信息，ID: {}", id);

        return houseService.deleteHouseById(id);

    }

    /**
     * 获取所有房屋朝向
     * @return orientationList
     */
    @GetMapping("/house/orientation")
    @Operation(summary = "获取所有房屋的朝向")
    public Result getAllOrientation() {
        log.info("获取所有房屋的朝向");
        List<String> orientationList = houseService.selectAllOrientation();
        return Result.success(orientationList);
    }

    /**
     * 更新房屋租金
     * @param id
     * @param rent
     * @return msg
     */
    @PutMapping("/house/update/rent")
    @Operation(summary = "更新房屋租金")
    public Result updateHouseRent(@RequestParam("id") Long id,
                                  @RequestParam("rent") Double rent) {
        log.info("更新房屋租金");
        String msg = houseService.updateHouseRent(id, rent);
        return Result.success(msg);
    }

    /**
     * 获取所有房屋ID
     * @return IDList
     */
    @GetMapping("/house/id")
    @Operation(summary = "获取所有房屋ID")
    public Result getAllHouseId() {
        log.info("获取所有房屋ID");
        List<Long> IDList = houseService.getAllHouseId();
        if (IDList == null){
            return Result.error("获取所有房屋ID失败");
        }
        return Result.success(IDList);
    }
    /**
     * 编辑房屋信息
     * @param houseDTO
     * @return
     */
    @PutMapping("/house/edit")
    @Operation(summary = "编辑房屋信息")
    public Result editHouse(@RequestBody HouseDTO houseDTO) {
        log.info("编辑房屋信息");
        String msg = houseService.editHouse(houseDTO);
        return Result.success(msg);
    }
    /**
     * 获取单个房屋信息（查询回显）
     * @return houseVO
     */
    @GetMapping("/house/detail/{id}")
    @Operation(summary = "获取单个房屋信息（查询回显）")
    public Result getHouseById(@PathVariable("id") Long id) {
        log.info("获取单个房屋信息（查询回显）");
        HouseVO houseVO = houseService.getHouseById(id);
        if (houseVO == null){
            return Result.error("获取房屋信息失败");
        }
        return Result.success(houseVO);
    }
    /**
     * 批量删除房屋信息
     * @return
     */
    @PostMapping("/house/delete/batch")
    @Operation(summary = "批量删除房屋信息")
    public Result batchDeleteHouse(@RequestBody List<Long> ids) {
        log.info("批量删除房屋信息");
        String msg = houseService.batchDeleteHouse(ids);
        return Result.success(msg);
    }

    /**
     * 获取房屋详情（提供于AI调用的专属API）
     * @return AiHouseVO
     */
    @GetMapping("/detail/ai")
    @Operation(summary = "获取房屋详情（提供于AI调用的专属API）")
    public Result getHouseDetailForAI(AiHouseDTO aiHouseDTO) {
        log.info("获取房屋详情（提供于AI调用的专属API）:{}",aiHouseDTO);
        List<AiHouseVO> aiHouseVOList = houseService.getHouseDetailForAI(aiHouseDTO);
        return Result.success(aiHouseVOList);
    }


}
